package com.itsu.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itsu.domain.ModuleTree;
import com.itsu.dao.RoleDao;
import com.itsu.domain.Role;
import com.itsu.domain.RoleTree;
import com.itsu.service.RoleService;
import com.itsu.utils.MapperUtils;

import java.util.List;

public class RoleServiceImpl implements RoleService {
    
    private RoleDao mapper;

    @Override
    public void save(Role role) {
        try {
            mapper = MapperUtils.getMapper(RoleDao.class);
            mapper.save(role);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public void delete(Role role) {
        try {
            mapper = MapperUtils.getMapper(RoleDao.class);
            mapper.delete(role);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public void update(Role role) {
        try {
            mapper = MapperUtils.getMapper(RoleDao.class);
            mapper.update(role);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public Role findById(String id) {
        try {
            mapper = MapperUtils.getMapper(RoleDao.class);
            Role role = mapper.findById(id);
            return role;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public List<Role> findAll() {
        try {
            mapper = MapperUtils.getMapper(RoleDao.class);
            List<Role> all = mapper.findAll();
            return all;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public PageInfo<Role> findAll(int pageNum, int pageSize) {
        try {
            mapper = MapperUtils.getMapper(RoleDao.class);
            PageHelper.startPage(pageNum, pageSize);
            List<Role> all = mapper.findAll();
            PageInfo<Role> RolePageInfo = new PageInfo<>(all);
            return RolePageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public List<ModuleTree> getModuleByRoleId(String roleId) {
        try {
            mapper = MapperUtils.getMapper(RoleDao.class);
            List<ModuleTree> list = mapper.getModuleByRoleId(roleId);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public void deleteModuleByRoleId(String roleId) {
        try {
            mapper = MapperUtils.getMapper(RoleDao.class);
            mapper.deleteModuleByRoleId(roleId);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public void addModuleRole(String roleId, String moduleId) {
        try {
            mapper = MapperUtils.getMapper(RoleDao.class);
            mapper.addModuleRole(roleId, moduleId);
            MapperUtils.commit();
        } catch (Exception e) {
            MapperUtils.rollback();
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
    }

    @Override
    public List<RoleTree> findRoleByUserId(String uid) {
        try {
            mapper = MapperUtils.getMapper(RoleDao.class);
            List<RoleTree> list = mapper.findRoleByUserId(uid);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MapperUtils.close();
        }
        return null;
    }
}
